use warp::Filter;
use crate::{config::Config, version::VersionManager, run, process};

pub async fn start_api_server() {
    let config = Config::load().unwrap();
    
    let list_models = warp::path!("models")
        .map(|| warp::reply::json(&list_available_models()));

    let run_model = warp::path!("run" / String)
        .and(warp::query::<HashMap<String, String>>())
        .map(|model, params| {
            let version = params.get("version");
            async move {
                match VersionManager::resolve_version(&model, version) {
                    Ok(v) => {
                        let path = VersionManager::get_model_path(&model, &v);
                        run::run_llama_model(path.to_str().unwrap()).await;
                        warp::reply::json(&"Model started")
                    }
                    Err(e) => warp::reply::json(&format!("Error: {}", e)),
                }
            }
        });

    let routes = list_models.or(run_model);
    
    warp::serve(routes)
        .run(([127, 0, 0, 1], 8080))
        .await;
}

fn list_available_models() -> Vec<String> {
    // 实现模型列表获取逻辑
    vec!["llama2".into(), "mistral".into()]
}